package router;

import javax.swing.JTabbedPane;

import router.base.client.Client;
import router.base.client.EventClient;
import router.base.client.MessageProcessor;
import router.base.linker.CSLinker;
import router.base.router.Router;
import router.base.router.RouterException;
import router.direct.DirectLinker;
import router.message.Message;
import router.message.StringMessage;
import router.messageFilters.EspionMessageFilter;
import router.messageFilters.MessageFilter;
import router.socket.SocketCSLinker;
import router.socket.SocketRouter;
import gui.MultiTabWindow;
import logger.Logger;
import logger.gui.FilterLogPanel;

public class Test {

	/**
	 * @param args
	 * @throws RouterException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws RouterException, InterruptedException {
		router_socket();
		
	}

	
	static void router_direct() throws RouterException, InterruptedException {
		Router router = new Router();
		
		MessageFilter filter = new EspionMessageFilter();
		
		DirectLinker linker1 = new DirectLinker("client1", router, filter);
		DirectLinker linker2 = new DirectLinker("client2", router, filter);
		
		final Logger logger1 = new Logger();
		final Logger logger2 = new Logger();
		
		MessageProcessor processor1 = new MessageProcessor() {
			@Override
			public void process(Message msg) {
				logger1.info("client1 : " + msg);
			}
		};
		MessageProcessor processor2 = new MessageProcessor() {
			@Override
			public void process(Message msg) {
				logger2.info("client2 : " + msg);
			}
		};
		
		
		Client client1 = EventClient.newMultiThreadClient(linker1, processor1);
		Client client2 = EventClient.newMultiThreadClient(linker2, processor2);
		
		MultiTabWindow window = new MultiTabWindow(JTabbedPane.LEFT);
		window.addTab(new FilterLogPanel(router.getLogger()), "Router");
		window.addTab(new FilterLogPanel(logger1), "Client1");
		window.addTab(new FilterLogPanel(logger2), "Client2");
		window.setVisible(true);
		
		

		Thread.sleep(2000);
		client2.send(new StringMessage(53, "Coucou c'est moi : 1"));
		Thread.sleep(200);
		client1.send(new StringMessage(53, "Coucou c'est moi : 2"));
		Thread.sleep(200);
		client2.send(new StringMessage(53, "Coucou c'est moi : 3"));
		Thread.sleep(200);
		client1.send(new StringMessage(53, "Coucou c'est moi : 4"));
	}
	
	static void router_socket() throws RouterException, InterruptedException {
		Router router = new SocketRouter(50000);
		
		MessageFilter filter = new EspionMessageFilter();
		
		final Logger logger1 = new Logger();
		final Logger logger2 = new Logger();		
		final Logger logger3 = new Logger();		
		
		CSLinker linker1 = new SocketCSLinker("client1", filter, logger1, "localhost", 50000);
		CSLinker linker2 = new SocketCSLinker("client2", filter, logger2, "localhost", 50000);
		CSLinker linker3 = new SocketCSLinker("client3", filter, logger3, "localhost", 50000);
		
		MessageProcessor processor1 = new MessageProcessor() {
			@Override
			public void process(Message msg) {
				logger1.info("client1 : " + msg);
			}
		};
		MessageProcessor processor2 = new MessageProcessor() {
			@Override
			public void process(Message msg) {
				logger2.info("client2 : " + msg);
			}
		};
		MessageProcessor processor3 = new MessageProcessor() {
			@Override
			public void process(Message msg) {
				logger3.info("client3 : " + msg);
			}
		};
		
		
		Client client1 = EventClient.newMultiThreadClient(linker1, processor1);
		Client client2 = EventClient.newSingleThreadClient(linker2, processor2);
		Client client3 = EventClient.newSingleThreadClient(linker3, processor3);
		
		MultiTabWindow window = new MultiTabWindow(JTabbedPane.LEFT);
		window.addTab(new FilterLogPanel(router.getLogger()), "Router");
		window.addTab(new FilterLogPanel(logger1), "Client1");
		window.addTab(new FilterLogPanel(logger2), "Client2");
		window.addTab(new FilterLogPanel(logger2), "Client3");
		window.setVisible(true);
		
		
		//Thread.sleep(200);
		client2.send(new StringMessage(53, "Coucou c'est moi : 2.1"));
		//Thread.sleep(200);
		client1.send(new StringMessage(53, "Coucou c'est moi : 1.1"));
		//Thread.sleep(200);
		client2.send(new StringMessage(53, "Coucou c'est moi : 2.2"));
		//Thread.sleep(200);
		client1.send(new StringMessage(53, "Coucou c'est moi : 1.2"));
		
		client3.send(new StringMessage(53, "Coucou c'est moi : 3.1"));
		//Thread.sleep(200);
		client3.send(new StringMessage(53, "Coucou c'est moi : 3.2"));
		//Thread.sleep(200);
		client3.send(new StringMessage(53, "Coucou c'est moi : 3.3"));
		//Thread.sleep(200);
		client3.send(new StringMessage(53, "Coucou c'est moi : 3.4"));
		
		Thread.sleep(200);
		client1.disconnect();
		Thread.sleep(2000);
		router.close();
	}
}
